package com.whm.hrmanagement.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import cn.hutool.core.date.DateUtil;
import com.whm.hrmanagement.domain.OaStaffQj;
import com.whm.hrmanagement.domain.vo.AttendanceMonthVO;
import com.whm.hrmanagement.domain.vo.OaAttendanceVo;
import com.whm.system.domain.dto.UserAttendanceDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.whm.common.annotation.Log;
import com.whm.common.core.controller.BaseController;
import com.whm.common.core.domain.AjaxResult;
import com.whm.common.enums.BusinessType;
import com.whm.hrmanagement.domain.OaStaffAttendance;
import com.whm.hrmanagement.service.IOaStaffAttendanceService;
import com.whm.common.utils.poi.ExcelUtil;
import com.whm.common.core.page.TableDataInfo;

/**
 * 员工考勤Controller
 * 
 * @author whm
 * @date 2024-02-06
 */
@RestController
@RequestMapping("/hrmanagement/attendance")
public class OaStaffAttendanceController extends BaseController
{
    @Autowired
    private IOaStaffAttendanceService oaStaffAttendanceService;




    /**
     * 查询员工考勤列表
     */
    @PreAuthorize("@ss.hasPermi('hrmanagement:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaAttendanceVo oaStaffAttendance)
    {
        startPage();
        List<UserAttendanceDto> list = oaStaffAttendanceService.selectOaStaffAttendanceList2(oaStaffAttendance);
        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setNum(29);

        return tableDataInfo;
    }

    /**
     * 导出员工考勤列表
     */
    @PreAuthorize("@ss.hasPermi('hrmanagement:attendance:export')")
    @Log(title = "员工考勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaAttendanceVo oaAttendanceVo)
    {

        List<AttendanceMonthVO> list = oaStaffAttendanceService.export(oaAttendanceVo);
        ExcelUtil<AttendanceMonthVO> util = new ExcelUtil<AttendanceMonthVO>(AttendanceMonthVO.class);
        String month =oaAttendanceVo.getMonth();
        if (month == null) {
            month = DateUtil.format(new java.util.Date(), "yyyyMM");
        }
        String yearMonth = month.substring(0, 4) + "年" + month.substring(4) + "月";
        util.exportExcel(response, list, "员工考勤数据",yearMonth+"考勤数据表");
    }

    /**
     * 获取员工考勤详细信息
     */
    @PreAuthorize("@ss.hasPermi('hrmanagement:attendance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaStaffAttendanceService.selectOaStaffAttendanceById(id));
    }

    /**
     * 新增员工考勤
     */
    @PreAuthorize("@ss.hasPermi('hrmanagement:attendance:add')")
    @Log(title = "员工考勤", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaStaffAttendance oaStaffAttendance)
    {
        return toAjax(oaStaffAttendanceService.insertOaStaffAttendance(oaStaffAttendance));
    }

    /**
     * 修改员工考勤
     */
    @PreAuthorize("@ss.hasPermi('hrmanagement:attendance:edit')")
    @Log(title = "员工考勤", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaStaffAttendance oaStaffAttendance)
    {
        return toAjax(oaStaffAttendanceService.updateOaStaffAttendance(oaStaffAttendance));
    }

    /**
     * 删除员工考勤
     */
    @PreAuthorize("@ss.hasPermi('hrmanagement:attendance:remove')")
    @Log(title = "员工考勤", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oaStaffAttendanceService.deleteOaStaffAttendanceByIds(ids));
    }



    @GetMapping("/all")
    public AjaxResult findAll() {

        return success(oaStaffAttendanceService.findAll());
    }


    @PutMapping("/set")
    public AjaxResult setAttendance(@RequestBody OaStaffAttendance oaStaffAttendance) {
        return toAjax(oaStaffAttendanceService.setAttendance(oaStaffAttendance));
    }


    @GetMapping("/staff/{id}/{date}")
    public AjaxResult findByStaffIdAndDate(@PathVariable Long id,@PathVariable String date) {
        OaStaffAttendance oaStaffAttendance = oaStaffAttendanceService.findByStaffIdAndDate(id,date);
        if (oaStaffAttendance ==null){
            return success("无当前考勤状态");
        }


        return success(oaStaffAttendanceService.findByStaffIdAndDate(id,date));
    }
}
